<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="视频描述" prop="description">
        <el-input
          v-model="queryParams.description"
          placeholder="请输入视频描述"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发布人" prop="nickname">
        <el-input
          v-model="queryParams.nickname"
          placeholder="请输入发布人"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否公开" prop="isPublic">
        <el-select v-model="queryParams.isPublic" placeholder="请选择" clearable>
          <el-option label="公开" value="1" />
          <el-option label="私密" value="0" />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option label="审核中" value="0" />
          <el-option label="审核通过" value="1" />
          <el-option label="不通过" value="2" />
          <el-option label="已封禁" value="3" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['cms:short:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="shortList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="shortId" width="80" />
      <el-table-column label="封面" align="center" prop="poster" width="100" fixed="left">
        <template #default="scope">
          <el-image
            style="width: 60px; height: 80px"
            :src="scope.row.poster"
            :preview-src-list="[scope.row.poster]"
            fit="cover"
            v-if="scope.row.poster"
          >
            <template #error>
              <div class="image-slot">
                <el-icon><VideoPlay /></el-icon>
              </div>
            </template>
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="视频描述" align="center" prop="description" :show-overflow-tooltip="true" min-width="150" />
      <el-table-column label="时长" align="center" prop="duration" width="80">
        <template #default="scope">
          {{ formatDuration(scope.row.duration) }}
        </template>
      </el-table-column>
      <el-table-column label="大小" align="center" prop="size" width="100">
        <template #default="scope">
          {{ formatSize(scope.row.size) }}
        </template>
      </el-table-column>
      <el-table-column label="比例" align="center" width="100">
        <template #default="scope">
          {{ scope.row.width }} × {{ scope.row.height }}
        </template>
      </el-table-column>
      <el-table-column label="点赞数" align="center" prop="likes" width="80" />
      <el-table-column label="评论数" align="center" prop="comments" width="80" />
      <el-table-column label="分享数" align="center" prop="shareCount" width="80" />
      <el-table-column label="收藏数" align="center" prop="collection" width="80" />
      <el-table-column label="是否公开" align="center" prop="isPublic" width="100">
        <template #default="scope">
          <el-tag v-if="scope.row.isPublic === '1'" type="success">公开</el-tag>
          <el-tag v-else type="warning">私密</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" width="100">
        <template #default="scope">
          <el-tag v-if="scope.row.status === '0'" type="info">审核中</el-tag>
          <el-tag v-else-if="scope.row.status === '1'" type="success">审核通过</el-tag>
          <el-tag v-else-if="scope.row.status === '2'" type="warning">不通过</el-tag>
          <el-tag v-else-if="scope.row.status === '3'" type="danger">已封禁</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="发布人" align="center" prop="memberUser.nickname" width="120" />
      <el-table-column label="发布时间" align="center" prop="createTime" width="160">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="160">
        <template #default="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" min-width="100" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="100" fixed="right">
        <template #default="scope">
          <el-button link type="primary" icon="View" @click="handleAudit(scope.row)" v-hasPermi="['cms:short:audit']">审核</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 审核对话框 -->
    <el-dialog title="审核" v-model="auditOpen" width="600px" append-to-body>
      <el-form ref="auditRef" :model="auditForm" :rules="auditRules" label-width="100px">
        <el-form-item label="视频描述">
          <el-input v-model="auditForm.description" disabled />
        </el-form-item>
        <el-form-item label="审核结果" prop="status">
          <el-select v-model="auditForm.status" placeholder="请选择审核结果">
            <el-option label="审核中" value="0" />
            <el-option label="审核通过" value="1" />
            <el-option label="不通过" value="2" />
            <el-option label="已封禁" value="3" />
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" v-model="auditForm.remark" placeholder="请输入备注" :rows="4" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="auditOpen = false">取 消</el-button>
          <el-button type="primary" @click="submitAudit">确 定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Short">
import { listShort, auditShort } from "@/api/cms/short";
import { getCurrentInstance, ref, reactive, toRefs } from 'vue';
import { VideoPlay } from '@element-plus/icons-vue';

const { proxy } = getCurrentInstance();

const shortList = ref([]);
const auditOpen = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const total = ref(0);

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    description: null,
    nickname: null,
    isPublic: null,
    status: null
  },
  auditForm: {
    shortId: null,
    description: '',
    status: null,
    remark: null
  },
  auditRules: {
    status: [
      { required: true, message: "请选择审核结果", trigger: "change" }
    ]
  }
});

const { queryParams, auditForm, auditRules } = toRefs(data);

/** 格式化时长 */
function formatDuration(seconds) {
  if (!seconds) return '-';
  const m = Math.floor(seconds / 60);
  const s = seconds % 60;
  return `${m}:${String(s).padStart(2, '0')}`;
}

/** 格式化文件大小 */
function formatSize(bytes) {
  if (!bytes) return '-';
  return `${(bytes / 1024 / 1024).toFixed(1)}MB`;
}

/** 查询短视频列表 */
function getList() {
  loading.value = true;
  listShort(queryParams.value).then(response => {
    shortList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.shortId);
}

/** 审核按钮操作 */
function handleAudit(row) {
  auditForm.value = {
    shortId: row.shortId,
    description: row.description,
    status: row.status,
    remark: row.remark || ''
  };
  auditOpen.value = true;
}

/** 提交审核 */
function submitAudit() {
  proxy.$refs["auditRef"].validate(valid => {
    if (valid) {
      auditShort(auditForm.value.shortId, auditForm.value.status, auditForm.value.remark).then(response => {
        proxy.$modal.msgSuccess("审核成功");
        auditOpen.value = false;
        getList();
      });
    }
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('cms/short/export', {
    ...queryParams.value
  }, `short_${new Date().getTime()}.xlsx`)
}

getList();
</script>

<style scoped>
.image-slot {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  background: #f5f7fa;
  color: #909399;
  font-size: 20px;
}
</style>